package com.zhoujinlong.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.android.core.ui.BaseFragment;
import com.zhoujinlong.R;
import com.zhoujinlong.ui.activity.ListActivity;
import com.zhoujinlong.ui.widget.TitleBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author: 蜡笔小新
 * @date: 2016-05-31 10:51
 * @GitHub: https://github.com/meikoz
 */
public class HomeFragment extends BaseFragment {


    @Bind(R.id.gridView)
    GridView mGridView;
    @Bind(R.id.title_bar)
    TitleBar mTitleBar;

    private List<Map<String, Object>> data_list;
    private SimpleAdapter sim_adapter;
    // 图片封装为一个数组
    private int[] icon = {R.drawable.icon_exercise_0, R.drawable.icon_exercise_1,
            R.drawable.icon_exercise_2, R.drawable.icon_exercise_3, R.drawable.icon_exercise_4,
            R.drawable.icon_exercise_5};
    private String[] iconName;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_home;
    }

    @Override
    protected void onInitView() {

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mTitleBar.setBackFinishEnable(false);
        mTitleBar.setTitle(R.string.tab_text_home);
        iconName = new String[]{getString(R.string.category1), getString(R.string.category2), getString(R.string.category3), getString(R.string.category4), getString(R.string.category5), getString(R.string.category6)};

        //新建List
        data_list = new ArrayList<Map<String, Object>>();
        //获取数据
        getData();
        //新建适配器
        String[] from = {"image", "text"};
        int[] to = {R.id.iv_exercise, R.id.tv_exercise_title};
        sim_adapter = new SimpleAdapter(getActivity(), data_list, R.layout.item_exercise, from, to);
        //配置适配器
        mGridView.setAdapter(sim_adapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListActivity.start(getActivity(), position + 1);
            }
        });
    }

    public void getData() {
        //cion和iconName的长度是相同的，这里任选其一都可以
        for (int i = 0; i < icon.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", icon[i]);
            map.put("text", iconName[i]);
            data_list.add(map);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
